﻿
namespace VS.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using VS.Web.Model;


    // Implements application logic using the WebContext context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class ArticlesService : LinqToEntitiesDomainService<WebContext>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Articles' query.
        public IQueryable<Article> GetArticles()
        {
            return this.ObjectContext.Articles;
        }

        public void InsertArticle(Article article)
        {
            if ((article.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(article, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Articles.AddObject(article);
            }
        }

        public void UpdateArticle(Article currentArticle)
        {
            this.ObjectContext.Articles.AttachAsModified(currentArticle, this.ChangeSet.GetOriginal(currentArticle));
        }

        public void DeleteArticle(Article article)
        {
            if ((article.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(article, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Articles.Attach(article);
                this.ObjectContext.Articles.DeleteObject(article);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Tags' query.
        public IQueryable<Tag> GetTags()
        {
            return this.ObjectContext.Tags;
        }

        public void InsertTag(Tag tag)
        {
            if ((tag.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tag, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Tags.AddObject(tag);
            }
        }

        public void UpdateTag(Tag currentTag)
        {
            this.ObjectContext.Tags.AttachAsModified(currentTag, this.ChangeSet.GetOriginal(currentTag));
        }

        public void DeleteTag(Tag tag)
        {
            if ((tag.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tag, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Tags.Attach(tag);
                this.ObjectContext.Tags.DeleteObject(tag);
            }
        }
    }
}


